package top.shenluw.springdemo.webflux;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * @author Shenluw
 * 创建日期：2019/1/22 15:09
 */
@Component
public class UserHandler {
    public Mono<ServerResponse> getUser(ServerRequest request) {
        return ServerResponse.ok().syncBody("i am getUser");
    }
}

@Configuration
class RoutingConfiguration {

    @Bean
    public RouterFunction<ServerResponse> monoRouterFunction(UserHandler userHandler) {
        return route(GET("/handler").and(accept(APPLICATION_JSON)), userHandler::getUser);
    }

}